package com.trinity.orders.domain.state;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.orders.constant.OrderConstants;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.service.IXcOrdersService;
import lombok.Data;

import javax.annotation.Resource;

/**
 * 账单得状态
 * NOTPAY 新订单
 * USERPAYING     支付中
 * ORDERPAID     已支付
 * CLOSED 已取消
 * REFUND 退款中
 * FULLREFUND 已全额退款
 * PARTIALREFUND 部分退款
 */
class NotPayState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("新订单");
    }
    @Override
    public String getStatus() {
        return "NOTPAY";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

class UserPayingState implements OrderState {
    @Resource
    private IXcOrdersService ordersService;
    @Override
    public void processOrder(OrderContext order) {
        System.out.println("支付中");
        // 在已发货订单状态下的处理逻辑
    }
    @Override
    public String getStatus() {
        return "USERPAYING";
    }

    @Override
    public AjaxResult requestPayment(OrderContext context) {
        try {
            return ordersService.requestPayment("id","openid");
        } catch (WxPayException e) {
            throw new RuntimeException(e);
        }
    }
}

class PaidState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("已支付");

    }

    @Override
    public String getStatus() {
        return "ORDERPAID";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

class ClosedState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("已取消");
    }
    @Override
    public String getStatus() {
        return "CLOSED";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

class RefundState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("退款中");
    }

    @Override
    public String getStatus() {
        return "REFUND";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

class FullRefundState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("部分退款");
    }

    @Override
    public String getStatus() {
        return "FULLREFUND";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

class PartialRefundState implements OrderState {

    @Override
    public void processOrder(OrderContext order) {
        System.out.println("部分退款");

    }

    @Override
    public String getStatus() {
        return "PARTIALREFUND";
    }

    @Override
    public AjaxResult requestPayment(OrderContext order) {
        return null;
    }
}

/**
 * @author 65720
 */
@Data
public class OrderContext {

    private OrderState state;

    private XcOrders order;

    public OrderContext(XcOrders order) {
        this.order = order;
        if (OrderConstants.OrderState.NOTPAY.equals(order.getStatus())) {
            //未支付
            this.state = new NotPayState();
        }
        if (OrderConstants.TradeState.SUCCESS.equals(order.getStatus())) {
            //已
            this.state = new PaidState();
        }
        if (OrderConstants.TradeState.REFUND.equals(order.getStatus())) {
            //已退款 也是支付
            this.state = new PartialRefundState();
        }
    }

    public void process() {
        state.processOrder(this);
    }

    /**
     * 进行支付
     */
    public void pay() {
//        state.pay(this);
    }

    public String queryStatus() {
        return state.getStatus();
    }
}

/**
 * @author 65720
 */
class Main {
    public static void main(String[] args) {
        XcOrders order1 = new XcOrders();
        order1.setStatus(OrderConstants.OrderState.NOTPAY);
        OrderContext order = new OrderContext(order1);
        System.out.println(order.queryStatus()); // 处理新订单
        order.process(); // 处理已发货订单
        order.process(); // 处理已交付订单
    }
}
